package com.jzy.chatglmsdk18753goldn.model.Qwen;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jzy.chatglmsdk18753goldn.model.ChatCompletionCommonRequest;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Data
@Slf4j
@JsonInclude(JsonInclude.Include.NON_NULL)
@NoArgsConstructor
@AllArgsConstructor
public class QwenChatCompletionRequest extends ChatCompletionCommonRequest {

//    @JsonProperty("stream_options")
//    private StreamOption streamOptions;

    /**
     * 采样候选集的大小
     */
    @JsonProperty("top_k")
    private Integer topK;

    /**
     * 是否开启思考模式
     */
    @JsonProperty("enable_thinking")
    private Boolean enableThinking;

    /**
     * 思考过程的最大长度
     */
    @JsonProperty("thinking_budget")
    private Boolean thinkingBudget;

    /**
     * 设置seed参数会使文本生成过程更具有确定性，通常用于使模型每次运行的结果一致。
     * 取值范围：0到2^31−1。
     */
    private Integer seed;

    /**
     * 模型在生成文本时是否使用互联网搜索结果进行参考
     * true：启用互联网搜索
     * false（默认）
     */
    @JsonProperty("enable_search")
    private Boolean enableSearch = Boolean.FALSE;

    @JsonProperty("search_options")
    private SearchOption searchOptions;

//    private class StreamOption {
//    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private class SearchOption {
        @JsonProperty("forced_search")
        private Boolean forcedSearch = Boolean.FALSE;
        @JsonProperty("search_strategy")
        private String searchStrategy = "turbo";
        @JsonProperty("enable_search_extension")
        private Boolean enableSearchExtension = Boolean.FALSE;
    }

    @Override
    public String toString() {
        try {
            Map<String, Object> paramsMap = new HashMap<>();
            if (null != this.getRequestId()) {
                paramsMap.put("request_id", this.getRequestId());
            }
            if(null != this.getResponseFormat()){
                paramsMap.put("response_format", this.getResponseFormat());
            }
            if(null != this.getTopP()){
                paramsMap.put("top_p", this.getTopP());
            }
            if (null != this.getStop() && this.getStop().size() > 0) {
                paramsMap.put("stop", this.getStop());
            }
            if (null != this.getTools() && this.getTools().size() > 0) {
                paramsMap.put("tools", this.getTools());
                paramsMap.put("tool_choice", this.getToolChoice());
            }
            if (null == this.getMessages()) {
                throw new RuntimeException("One of messages or prompt must not be empty！");
            }
            paramsMap.put("stream", this.getStream());
            paramsMap.put("messages", this.getMessages());
            paramsMap.put("model", this.getModel().getCode());
            paramsMap.put("temperature", this.getTemperature());
            paramsMap.put("max_tokens", this.getMaxTokens());
            return new ObjectMapper().writeValueAsString(paramsMap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }



}
